/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.lucava.boatskeeper.client.presenter.boat;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasWidgets;
import net.customware.gwt.dispatch.client.DispatchAsync;
import net.customware.gwt.presenter.client.EventBus;
import net.customware.gwt.presenter.client.place.Place;
import net.customware.gwt.presenter.client.place.PlaceRequest;
import net.customware.gwt.presenter.client.widget.WidgetDisplay;
import org.lucava.boatskeeper.client.MainLayout;
import org.lucava.boatskeeper.client.PlaceMapper;
import org.lucava.boatskeeper.client.event.ShowCancelledEvent;
import org.lucava.boatskeeper.client.presenter.ViewPresenter;
import org.lucava.boatskeeper.shared.Boat;
import org.lucava.boatskeeper.shared.rpc.boat.GetBoat;
import org.lucava.boatskeeper.shared.rpc.boat.GetBoatResult;

/**
 *
 * @author glenn
 */
public class ShowBoatPresenter extends ViewPresenter<ShowBoatPresenter.Display> {

    public interface Display extends WidgetDisplay {

        public HasClickHandlers getReturnButton();

        public HasText getDisplayName();
    }
    private Boat boat;
    public static Place PLACE;

    public ShowBoatPresenter(DispatchAsync dispatcher, EventBus eventBus, Display display) {
        super(dispatcher, eventBus, display);
        bind();
    }

    @Override
    protected void onBind() {
        this.display.getReturnButton().addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                boat = null;
                eventBus.fireEvent(new ShowCancelledEvent(PlaceMapper.LIST_BOAT));
            }
        });

    }

    private void getBoat(Integer id) {
        System.out.println(">>>>>>>>>>>>> displayBoat(String id)");
        getDispatcher().execute(new GetBoat(id), new AsyncCallback<GetBoatResult>() {
            @Override
            public void onSuccess(GetBoatResult result) {
                boat = result.getBoat();
                refreshDisplay();

            }

            @Override
            public void onFailure(Throwable caught) {
                // TODO Auto-generated method stub
            }
        });

    }

    @Override
    protected void onPlaceRequest(PlaceRequest request) {

        System.out.println(">>>>>>>>>>>EditBoatPresenter.onPlaceRequest");
        String boatId = request.getParameter("id", null);
        if (boatId != null) {
            getBoat(Integer.parseInt(boatId));
        } else {
            refreshDisplay();
        }
    }

    @Override
    public void refreshDisplay() {
        if (boat != null) {
            display.getDisplayName().setText(boat.getDisplayName());
        } else {
            display.getDisplayName().setText(null);
        }


    }

    @Override
    public Place getPlace() {
        return PLACE;
    }

    @Override
    protected void onUnbind() {
        // TODO Auto-generated method stub
    }

    @Override
    public void revealDisplay() {
        // TODO Auto-generated method stub
    }
}